package org.chaos.controller;

import org.chaos.mappers.AdminUserMapper;
import org.chaos.model.AdminUser;
import org.chaos.service.AdminService;
import org.chaos.utils.transaction.TransUtils;
import org.chaos.utils.transaction.Transaction;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class AdminController {
    @Resource
    private AdminService adminService;
    @Resource
    AdminUserMapper adminUserMapper;

    @PostMapping(value = "/addUser")
    public String test(@RequestBody AdminUser adminUser){
        String result = "success";
        Transaction trans = TransUtils.getTrans();
        try{
            trans.begin();
            adminService.addAdminUser(adminUser);
            if("张三".equals(adminUser.getUsername())){
                throw new RuntimeException("测试错误回滚");
            }
            trans.commit();
        }catch (Exception e){
            trans.rollback();
            result = e.getMessage();
        }
        return result;
    }
    @GetMapping("/test")
    public String test1(){
        return adminUserMapper.selectByExample(null).get(0).getNickname();
    }
}
